package com.zking.controller;

import com.zking.dao.IUserDao;
import com.zking.entity.Order;
import com.zking.entity.User;
import com.zking.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController @RequiredArgsConstructor
// @Validated @Valid
@Validated // 开启验证注解支持
public class MyController2 {
    private final IUserDao userDao;
    
    @GetMapping("/t3")
    public List<User> test3(@RequestParam(name = "page", defaultValue = "1") int pageNum) {
        return userDao.findAll();
    }
    
    @GetMapping("/t4")
    public org.springframework.data.domain.Page<User> test4(
            @RequestParam(name = "page", defaultValue = "1") int pageNum) {
        // page是从0开始
        return userDao.findAll(Pageable
                .ofSize(3)
                .withPage(pageNum - 1));
    }
    
    @GetMapping("/t5")
    public Page<User> test5(@RequestParam(name = "page", defaultValue = "1") int pageNum) {
        // Hibernate: select user0_.id as id1_0_,
        //  user0_.money as money2_0_,
        //  user0_.password as password3_0_,
        //  user0_.username as username4_0_
        //      from user2 user0_
        //      where (user0_.username like ? escape ?) and (user0_.money between ? and ?) limit ?
        return userDao.searchUsersByUsernameLikeAndMoneyBetween(Pageable
                .ofSize(3)
                .withPage(pageNum - 1), "%admin%", 100, 200);
    }
    
    /*
    常用的：
        @NotNull 不能不传，不能为null
        @NotBlank 不能为空串
        @Min(6) @Max(16) 长度
        @Size(min = 6, max = 16) 数字大小
        @Pattern(regexp = "^1[0-9]{10}$") 自定义的正则
     */
    @GetMapping("/t6")
    public Page<User> test6(
            @NotNull String name, // 不能为空，但是可以为""
            @NotBlank @NotNull @Min(6) @Max(16) String pass, // 不能是空字符串，长度要求
            @NotNull @Size(min = 6, max = 16) String address, // size表示大小，不是长度
            @NotNull @Digits(integer = 3, fraction = 0) Integer min, // 数字范围：integer，fraction
            @DecimalMin("0.0") @DecimalMax("999.999") Double max,  // 金钱范围
            @Email @NotNull String email, // 必须合理的邮箱
            @Pattern(regexp = "^1[0-9]{10}$") @NotNull String telephone, // 自定义规则
            @RequestParam(name = "page", defaultValue = "1") int pageNum
    ) {
        return userDao.searchUsersByUsernameLikeAndMoneyBetween(Pageable
                .ofSize(3)
                .withPage(pageNum - 1), "%admin%", 100, 200);
    }
}
